﻿using CoreShop.Library;
using CoreShop.Model;
using CoreShop.Services;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class frmDeskSpellSelect : UIForm
    {
        private long deskId = 0;
        private int operateType = 0;
        public long orderId = -1;
        private List<corecmsfoodorderingmaster> orders;
        public corecmsfoodorderingmaster orderingMaster = null;

        public frmDeskSpellSelect(long _deskId,int _operateType, List<corecmsfoodorderingmaster> _orders)
        {
            deskId = _deskId;
            operateType = _operateType;
            orders = _orders;
            InitializeComponent();
        }

        private void BindTableList()
        {
            var desk = new corecmsfooddeskServices().GetById(deskId);
            if (desk == null)
            {
                UIMessageBox.Show("选择的桌台信息不存在。", "系统出错:", UIStyle.Orange, UIMessageBoxButtons.OK);
                return;
            }
            Thread.Sleep(100);
            layTestList.Visible = false;
            layTestList.Clear();
            foreach (var order in orders)
            {
                UITableItem item = new UITableItem();
                item.BackColor = System.Drawing.Color.White;
                item.TableName = CommFunc.FormatDeskSpellNumber(desk.name, order.number);
                item.Tag = order.orderId.ToString();

                TimeSpan d3 = DateTime.Now.Subtract(order.orderDate);
                string times = string.Format("{0}分钟", d3.Days * 24 * 60 + d3.Hours * 60 + d3.Minutes);
                item.Times = times;
                item.Money = order.sumAllAmount.ToString("¥0.##");
                item.Person = order.person.ToString() + "人";
                if (order.foodCount == 0)
                {
                    item.TableStatus = EnumHelper.ToObject<GlobalEnumVars.TableStatus>((int)GlobalEnumVars.TableStatus.TobePlaced);
                }
                else
                {
                    item.TableStatus = EnumHelper.ToObject<GlobalEnumVars.TableStatus>(order.status); 
                }

                AddItem(item);
            }
            layTestList.Visible = true;
        }

        public void AddItem(UITableItem item)
        {
            item.TableClick += Item_TableClick;
            layTestList.Add(item);
            StatusFormStepIt();
        }

        private void FTables_Shown(object sender, System.EventArgs e)
        {
            BindTableList();
        }
      
        private void Item_TableClick(object sender, System.EventArgs e)
        {
            var item = (UITableItem)sender;
            orderId = CommFunc.ConvertToInt64(item.Tag);
            orderingMaster = orders.Where(p=>p.orderId == orderId).FirstOrDefault();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
                
        private void btnExit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void btnTableTypeAll_Click(object sender, EventArgs e)
        {

        }
    }
}
